// pages/huxin/huxin.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getUrl: app.data.url +'/mapi/index/getproduct',
    lingquurl:app.data.url + '/mapi/index/getcoujiang',
    shuju:[],
    linqu:true,
    fangjianid:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.setData({ fangjianid:id});
    var user = wx.getStorageSync('user');
    var that= this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.getUrl,
      method:'post',
      data:{token:user.token,id:id},
      success:function(res){
        var data = res.data;
        console.log(data);
        if(data.code==200){
          var shuju = data.data.shuju;
          console.log(shuju);
          that.setData({shuju:shuju});
        }else{
          wx.showToast({
            title: '房间没有礼品',
            icon:'none'
          })
          setTimeout(function(){
            wx.navigateBack({
              delta:1
            })
          },1000)
        }
        wx.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  lingqu:function(){
    var linqu = this.data.linqu;
    var user = wx.getStorageSync('user');
    var fangjianid = this.data.fangjianid;
    var that = this;
    if(!linqu){
      return;
    }
    that.setData({linqu:false});

    wx.showModal({
      title:'提示',
      content:'由于现在在测试阶段，请不要太当真，正式上线会发通告',
      success:function(th){
        if (th.confirm){
          wx.request({
            url: that.data.lingquurl,
            method: 'post',
            data: { token: user.token, fangjianid: fangjianid },
            success: function (res) {
              var data = res.data;
              console.log(data);
              if (data.code == 200) {
                wx.showModal({
                  title: '提示',
                  content: '获取礼品' + data.data.data.title,
                  cancelText: "继续",
                  confirmText: "礼品中心",
                  success: function (res) {
                    console.log(res);
                    if (res.confirm) {
                      wx.switchTab({
                        url: '/pages/jiangpin/jiangpin',
                      })
                    }
                  }
                })
              } else {
                wx.showToast({
                  title: data.msg,
                  icon: 'none'
                })
              }
              that.setData({ linqu: true });
            }
          })
        }else{
          that.setData({ linqu: true });
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var user = wx.getStorageSync('user');
    var titles = wx.getStorageSync('titles');
    return {
      title: titles,
      path: '/pages/index/index?scene' + user.id,
    };
  }
})